package com.mvc.dao;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.mvc.entity.Student;
import com.sun.istack.internal.FinalArrayList;

public class EntityDaoImpl extends HibernateDaoSupport implements EntityDao {

	public List<Object> createQuery(final String querysString) {
		return ((List<Object>) getHibernateTemplate().execute(new HibernateCallback<Object>() {
			public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(querysString);
				List<Object> rows = query.list();
				return rows;
			}
		}));
	}

	public void delete(final Object model) {
		getHibernateTemplate().execute(new HibernateCallback<Object>() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				session.delete(model);
				return null;
			}
		});

	}

	public Object save(final Object model) {
		return getHibernateTemplate().execute(new HibernateCallback<Object>() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				session.save(model);
				return null;
			}
		});
	}

	public void update(final Object model) {
		getHibernateTemplate().execute(new HibernateCallback<Object>() {

			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				session.update(model);
				return null;
			}
		});
	}

	public Student selectById(final Integer id) {
		return (Student) getHibernateTemplate().execute(new HibernateCallback<Object>() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Student s = (Student) session.get(Student.class, id);
				return s;
			}
		});
	}

}
